package com.upper.service.ipother;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.lucksyc.utilindependent.resultview.core.data.ViewData;
import org.lucksyc.utilindependent.resultview.seed.deal.ViewBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bean.Computer;
import com.bean.ComputerList;
import com.componentbean.UserNginxSet;
import com.util.Util;

@Service
public class IpOtherInterfaceImpl implements IpOtherInterface {
//	private static final Logger log = LoggerFactory.getLogger(IpOtherInterfaceImpl.class);
	
	@Autowired
	ViewBuilder viewBuilder;
	
	@Autowired
	UserNginxSet userset;
	
	@Override
	public ViewData getComputerList() {
		Map<Integer, Computer> clist = userset.getComputerList().getAll();
		if (null == clist) {
			return viewBuilder.wrapPage(null, 0);
		} else {
			List<Computer> list = new ArrayList<>(clist.size());
			Iterator<Entry<Integer, Computer>> it = clist.entrySet().iterator();
			while (it.hasNext()) {
				Entry<Integer, Computer> entry = it.next();
				list.add(entry.getValue());
			}
			return viewBuilder.wrapPage(list, 0);
		}
	}

	@Override
	public ViewData delComputer(String id) {
		if (Util.isPositiveInteger(id)) {
			Integer cid = Integer.parseInt(id);
			Map<Integer, Computer> clist = userset.getComputerList().getAll();
			if (null!= clist && clist.containsKey(cid)) {
				if (userset.getServiceList().isUsing(cid)) {
					return viewBuilder.wrapError("数据正在被使用", 10401);
				} else {
					Computer c = clist.get(cid);
					userset.getComputerList().getIpSet().remove(c.getIp());
					clist.remove(cid);
					return viewBuilder.wrap(true);
				}
			} else {
				return viewBuilder.wrapError("数据不存在", 10421);
			}
		} else {
			return viewBuilder.wrapError("参数不合法", 10499);
		}
	}
	
	@Override
	public ViewData saveComputer(String id, String name, String tag, String ip) {
		if (Util.isPositiveInteger(id)) {
			return editComputer(Integer.parseInt(id), name, tag, ip);
		} else {
			return addComputer(name, tag, ip);
		}
	}
	
	private ViewData addComputer(String name, String tag, String ip) {
		if (!Util.checkIP(ip)) {
			return viewBuilder.wrapError("IP不合法", 10101);
		}
		ComputerList computerList = userset.getComputerList();
		if (computerList.getIpSet().contains(ip)) {
			return viewBuilder.wrapError("IP已存在", 10102);
		}
		Integer cid = computerList.nextId();
		boolean existsId = true;
		for (int i = 0; i < 1000; i++) {
			if (computerList.getAll().containsKey(cid)) {
				cid = computerList.nextId();
			} else {
				existsId = false;
				break;
			}
		}
		if (existsId) {
			return viewBuilder.wrapError("数据占用，请重新操作", 10103);
		}
		Computer c = new Computer();
		c.setId(cid);
		c.setIp(ip);
		c.setName(name);
		c.setTag(tag);
		computerList.getAll().put(cid, c);
		computerList.getIpSet().add(ip);
		return viewBuilder.wrap(true);
	}

	private ViewData editComputer(Integer cid, String name, String tag, String ip) {
		if (!userset.getComputerList().getAll().containsKey(cid)) {
			return viewBuilder.wrapError("不存在的数据", 10501);
		}
		Computer c = userset.getComputerList().getAll().get(cid);
		boolean changed = false;
		if (null != name) {
			c.setName(name);
			changed = true;
		}
		if (null != tag) {
			c.setTag(tag);
			changed = true;
		}
		if (Util.checkIP(ip) && !ip.equals(c.getIp())) {
			userset.getComputerList().getIpSet().remove(c.getIp());
			c.setIp(ip);
			userset.getComputerList().getIpSet().add(ip);
			changed = true;
		}
		return viewBuilder.wrap(changed);
	}

}
